//
//  GC_GoodsTypesInfoView.m
//  WisdomConsumption
//
//  Created by mini3 on 2017/9/15.
//  Copyright © 2017年 WisdomConsumption. All rights reserved.
//

#import "GC_GoodsTypesInfoView.h"
#import "GC_GoodsTypesInfoCollectionViewCell.h"

@interface GC_GoodsTypesInfoView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

///Collection
@property (nonatomic, weak) UICollectionView *collectionView;
///数据
@property (nonatomic, strong) NSMutableArray *dataArray;
///选中 Id
@property (nonatomic, strong) NSString *selectId;

@end

@implementation GC_GoodsTypesInfoView

///初始化
- (id)init
{
    self = [super init];
    if(self){
        [self initDefault];
    }
    return self;
}

///初始
-(void)initDefault
{
    self.selectId = @"";
    self.frame = CGRectMake(0, 0, WIDTH_TRANSFORMATION(160), kMainScreenHeight);
    self.backgroundColor = kCommonBackgroudColor;
    [self loadSubViewAndConstraints];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

///加载子视图及约束
- (void)loadSubViewAndConstraints
{
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

///更新ui
- (void)updateUiForData
{
    [self.dataArray removeAllObjects];
    [self.dataArray addObjectsFromArray:@[@[@"0",@"零食"],@[@"1",@"服饰"],@[@"2",@"鞋包"],@[@"3",@"酒品"],@[@"4",@"数码"]]];
    
    if(self.dataArray.count > 0){
        self.selectId = @"0";
    }
    
    [self.collectionView reloadData];
}

- (void)updateUIForData:(NSMutableArray *)data{
    [self.dataArray removeAllObjects];
    [self.dataArray addObjectsFromArray:data];
    [self.collectionView reloadData];
}



#pragma mark -- UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    self.selectId = [NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"id"]];
    if(self.onSelectedBlock){
        self.onSelectedBlock(self.selectId);
    }
    
    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGSize size = CGSizeMake(WIDTH_TRANSFORMATION(160), HEIGHT_TRANSFORMATION(90));
    return size;
}

///设置行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return HEIGHT_TRANSFORMATION(0);
}

///设置列间距，配合item宽度来设置
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return WIDTH_TRANSFORMATION(0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GC_GoodsTypesInfoCollectionViewCell *cell = [GC_GoodsTypesInfoCollectionViewCell collectionView:collectionView cellForItemAtIndexPath:indexPath];
    [cell updateUiForDatas:self.dataArray[indexPath.row]];
//    GC_UserMobileResGoodsClassDataModel *model = self.dataArray[indexPath.row];
    NSString *string = [NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"id"]];
    if([string isEqualToString:self.selectId]){
        [cell setSelectedState:YES];
    }else{
        [cell setSelectedState:NO];
    }
    return cell;
}



#pragma mark -- getter,setter
///内容
- (UICollectionView *)collectionView
{
if(!_collectionView){
    UICollectionView *view = [UICollectionView createCollectionViewWithDelegateTarget:self];
    view.showsVerticalScrollIndicator = FALSE;
    view.showsHorizontalScrollIndicator = FALSE;
    
    view.backgroundColor = kFontColorWhite;
    [self addSubview:_collectionView = view];
    //注册
    [GC_GoodsTypesInfoCollectionViewCell registerCollectionView:view];
}
    return _collectionView;
}

- (NSMutableArray *)dataArray
{
    if(!_dataArray){
        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataArray;
}


@end
